﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using Douban.Modules.Conversation.Models;

namespace Douban.Modules.Conversation
{
    public class ChatLogFileManager
    {
        private readonly string _chatLogFileFolder;
        private readonly string _chatLogUserFileFolder;
        private readonly string _chatLogContactFileFolder;
        private IList<string> _chatLogDateFiles;
        private static ChatLogModel _chatLog;

        public ChatLogFileManager(string userId, string contactId)
        {
            _chatLogFileFolder = Path.Combine(
                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "豆Q"), "ChatLog");

            if (!Directory.Exists(_chatLogFileFolder))
                Directory.CreateDirectory(_chatLogFileFolder);

            int index = userId.LastIndexOf("/");
            _chatLogUserFileFolder = Path.Combine(_chatLogFileFolder, userId.Substring(index + 1));

            if (!Directory.Exists(_chatLogUserFileFolder))
                Directory.CreateDirectory(_chatLogUserFileFolder);

            index = contactId.LastIndexOf("/");
            _chatLogContactFileFolder = Path.Combine(_chatLogUserFileFolder, contactId.Substring(index + 1));

            if (!Directory.Exists(_chatLogContactFileFolder))
                Directory.CreateDirectory(_chatLogContactFileFolder);
        }

        public IList<string> ChatLogDateFiles
        {
            get
            {
                string[] filePaths = Directory.GetFiles(_chatLogContactFileFolder);
                Array.Sort(filePaths);

                _chatLogDateFiles = new List<string>();

                string fileName;
                foreach (var filePath in filePaths)
                {
                    fileName = Path.GetFileNameWithoutExtension(filePath);
                    _chatLogDateFiles.Add(fileName);
                }

                return _chatLogDateFiles;
            }
        }

        public void StoreChatLog(MessageModel[] messages, string date)
        {
            ChatLogModel chatLog = new ChatLogModel
            {
                Messages = messages
            };

            using (Stream stream = GetStream(date))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ChatLogModel));
                serializer.Serialize(stream, chatLog);
            }
        }

        public ChatLogModel GetChatLog(string date)
        {
            using (Stream stream = GetStream(date))
            {
                if (stream.Length == 0) return null;

                XmlSerializer serializer = new XmlSerializer(typeof(ChatLogModel));
                _chatLog = (ChatLogModel)serializer.Deserialize(stream);
            }

            return _chatLog;
        }

        private Stream GetStream(string date)
        {
            string chatLogDateFilePath = Path.Combine(_chatLogContactFileFolder, date + ".xml");

            return File.Open(chatLogDateFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
        }
    }
}
